import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { fileURLToPath, URL } from "node:url";
import replace from "@rollup/plugin-replace";
import terser from "@rollup/plugin-terser";

// https://vitejs.dev/config/
export default defineConfig({
	plugins: [vue()],

	build: {
		rollupOptions: {
			output: {
				manualChunks(id) {
					if (id.includes("node_modules")) {
						return id.toString().split("node_modules/")[1].split("/")[0].toString();
					}
				},
			},
			plugins: [
				// 去掉注释
				replace({
					preventAssignment: true,
					values: {
						__NO_COMMENTS__: "",
					},
					patterns: [
						{
							match: /\s*\/\/.*$/,
							replacement: "__NO_COMMENTS__",
						},
					],
				}),
				terser({
					// 生产环境移除console、debugger
					compress: {
						drop_console: true, // 默认false
						drop_debugger: true, // 默认true
					},
				}),
			],
		},
	},

	server: {
		hmr: {
			// 关闭错误提示浮层
			overlay: false,
		},
		port: 8081,
		strictPort: false,
		proxy: {
			"/api": {
				target: "http://192.168.2.70:15001/cax",
				changeOrigin: true,
				rewrite: (path) => path.replace(/^\/api/, ""),
			},
		},
	},

	resolve: {
		alias: {
			"@": fileURLToPath(new URL("./src", import.meta.url)),
		},
	},
});
